package com.ruoyi.cangchu.controller;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.cangchu.domain.TZhgdPersonAttnH;
import com.ruoyi.cangchu.service.ITZhgdPersonAttnHService;
import com.ruoyi.common.tool.PlatformTools;
import com.ruoyi.system.service.RFIDClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@EnableAsync
@RequestMapping("/personAttnMethod")
public class HumanGate {

    @Autowired
    private ITZhgdPersonAttnHService tZhgdPersonAttnHService;

    @Autowired
    private RFIDClientService rFIDClientService;

    /**
     * @return String
     * @Description 人闸认证结果推送
     * @author zwy
     * @date 2024/10/24 16:34
     */
    @PostMapping(value = "/HumanGatePush")
    public String HumanGatePush(@RequestBody Map<String, Object> param) {
        Map<String, Object> result = (Map<String, Object>)param.get("info");

        //插入考勤信息
        TZhgdPersonAttnH personAttnH = new TZhgdPersonAttnH();
        personAttnH.setId(PlatformTools.get20UUID());
        personAttnH.setUserId(result.get("PersonUUID").toString());
        personAttnH.setIdCard(result.get("IdCard").toString());
        personAttnH.setName(result.get("Name").toString());
        personAttnH.setAttnType(1l);
        personAttnH.setAttnStatus(1l);
        if (result.get("Direction") != null && !result.get("Direction").equals("")){
            personAttnH.setInoutFlag((int) result.get("Direction"));
            rFIDClientService.verify(result,personAttnH);   //开启仓储设备
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            Date date = dateFormat.parse(result.get("CreateTime").toString());
            personAttnH.setCollectTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        personAttnH.setDeviceNo(result.get("DeviceID").toString());
        personAttnH.setCreateTime(new Date());
        personAttnH.setDeleteFlag(0);
        tZhgdPersonAttnHService.insertTZhgdPersonAttnH(personAttnH);


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("desc", "OK");
        return jsonObject.toJSONString();
    }

    /**
     * @return String
     * @Description 人闸心跳
     * @author zwy
     * @date 2024/10/24 16:34
     */
    @PostMapping(value = "/heartbeat")
    public String heartbeat(@RequestBody Map<String, Object> param) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("desc", "OK");
        return jsonObject.toJSONString();
    }
}
